#include "config.h"
#include "ui_config.h"
#include "authmanager.h"

Config::Config(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Config)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();

    //Signals and Slots
    connect(bdf, &BllDataFlow::setWidgetVisibleSignal,this,&Config::setWidgetVisibleSlot);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &Config::retranslateUiSlot);

    map.insert(PARAM_SET,     ui->btnParamSet);
    map.insert(PERFORMANCE,   ui->btnPerformance);
    map.insert(SYSTEM_LOG,    ui->btnLog);
    map.insert(SYSTEM_CONFIG, ui->btnSystemConfig);
    map.insert(USER_MANAGE,   ui->btnUserManage);
    map.insert(ABOUT,         ui->btnAbout);
    map.insert(SYSTEM_UPDATE, ui->btnSysUpdate);
    map.insert(FACTORY,       ui->btnFactory);

    if (windowWidth >= 1280)
    {
        this->setProperty("cssSize", "large");
    }

    initWidget();
}

Config::~Config()
{
    delete ui;
}

void Config::showEvent(QShowEvent *)
{
    this->activateWindow();
}

void Config::initWidget()
{
    //默认显示系统设置-密码设置页面
    ui->btnSystemConfig->setChecked(true);
}

void Config::on_btnPerformance_toggled(bool checked)
{
    if (checked)
    {
        bdf->changeConfigPageIndex(ConfigPageIndex::PERFORMANCE);
    }
}

void Config::on_btnParamSet_toggled(bool checked)
{
    if (checked)
    {
        bdf->changeConfigPageIndex(ConfigPageIndex::PARAM_SET);
    }
}

void Config::on_btnSysUpdate_toggled(bool checked)
{
    if (checked)
    {
        bdf->changeConfigPageIndex(ConfigPageIndex::SYSTEM_UPDATE);
    }
}

void Config::on_btnLog_toggled(bool checked)
{
    if (checked)
    {
        bdf->changeConfigPageIndex(ConfigPageIndex::SYSTEM_LOG);
        bdf->setCurrentTime(TimeSetType::LOG_START);
    }
}

void Config::on_btnUserManage_toggled(bool checked)
{
    if (checked)
    {
        bdf->changeConfigPageIndex(ConfigPageIndex::USER_MANAGE);
    }
}

void Config::on_btnAbout_toggled(bool checked)
{
    if (checked)
    {
        bdf->changeConfigPageIndex(ConfigPageIndex::ABOUT);
    }
}

void Config::on_btnSystemConfig_toggled(bool checked)
{
    if (checked)
    {
        bdf->changeConfigPageIndex(ConfigPageIndex::SYSTEM_CONFIG);
    }
}

void Config::on_btnFactory_toggled(bool checked)
{
    if (checked)
    {
        bdf->changeConfigPageIndex(ConfigPageIndex::FACTORY);
    }
}

void Config::setWidgetVisibleSlot(int permission)
{
    AuthManager::instance()->showControls(ui->widget, permission);
    int index = AuthManager::instance()->showContainer(ui->widget);
    if (map.contains(index))
    {// 切换到指定页面
        QPushButton *button = map.value(index);
        emit button->toggled(true);
        button->setChecked(true);
    }
    else
    {
        bdf->changeConfigPageIndex(-1);
    }
}

void Config::retranslateUiSlot()
{
    ui->retranslateUi(this);
}
